<script>
	// 作者:百鬼夜行(18304636449)
	// #ifdef APP
	var Color = plus.android.importClass("android.graphics.Color");
	plus.android.importClass("android.view.Window");
	var mainActivity = plus.android.runtimeMainActivity();
	var window_android = mainActivity.getWindow();
	window_android.setNavigationBarColor(Color.parseColor("#fafafa"));
	// #endif
	import {
		setRequestValue
	} from '@/common/request.js'
	import storeInits from '@/store/storeInits.js'
	import routingIntercept from '@/utils/permission.js'
	import {
		pub,
		sub
	} from "./common/event.js"
	import commonData from "./common/common.js"
	import websocket from "./common/websocket.js"
	import auth from './utils/auth.js';
	let innerAudioContextdf = uni.createInnerAudioContext();
	let showfront = false
	export default {
		onLaunch: function() {
			uni.hideTabBar()
			const _this = this
			setTimeout(() => {
				// #ifdef APP
				plus.device.setWakelock(true);
				// #endif
			}, 1000)
			console.log('App Launch')
			// 初始化

			// 路由拦截器

			// 添加全局拦截器
			uni.addInterceptor('switchTab', {
				invoke(e) {
					// 在跳转前执行的逻辑
					console.log('switchTab 跳转前', e);
					_this.$store.commit(`tabbar/setCurrent`, e.url)
				},
				success(e) {
					// 在跳转成功后执行的逻辑
					console.log('switchTab 跳转成功', e);
				},
				fail(e) {
					// 在跳转失败时执行的逻辑
					console.log('switchTab 跳转失败', e);
				},
				complete(e) {
					// 在跳转结束时执行的逻辑（不论成功或失败）
					console.log('switchTab 跳转结束', e);
				}
			});

			// 将统一的请求导出
			setRequestValue({
				get: uni.$u.http.get,
				post: uni.$u.http.post,
				put: uni.$u.http.put,
				'delete': uni.$u.http.delete,
			})
			// store初始化
			storeInits.initAllStore({})
			// 注册全局事件

			sub("socket_getremoveuser", () => {
				uni.showToast({
					title: "被移除群聊",
					icon: "none"
				})
			})

			sub("socket_touser_delgroup", (e) => {
				uni.showToast({
					title: "群聊" + e.name + "已解散",
					icon: "none"
				})
				if (_this.$store.state.friend.current.gid > 0 && _this.$store.state.friend.current.gid == e
					.id) {
					uni.switchTab({
						url: "/pages/index/index"
					})
				}
			})

			sub("socket_touser_message", (e) => {
				if ((_this.$store.state.friend.current.fid > 0 && (_this.$store.state.friend.current.fid == e
						.toid || _this.$store.state.friend.current.fid == e.uid)) || (_this.$store.state.friend
						.current.gid > 0 && _this.$store.state.friend.current.gid == e.gid)) {
					return
				}
				_this.playSound()
			})

			sub("socket_togroup_message", (e) => {
				if ((_this.$store.state.friend.current.fid > 0 && (_this.$store.state.friend.current.fid == e
						.toid || _this.$store.state.friend.current.fid == e.uid)) || (_this.$store.state.friend
						.current.gid > 0 && _this.$store.state.friend.current.gid == e.gid)) {
					return
				}
				_this.playSound()
			})

			sub("socket_gvideocallinv", (e) => {
				const status = _this.$store.state.common.status
				console.log("statusstatus", status)
				if (status != "videocall") {
					_this.playSoundl()
					uni.navigateTo({
						url: "/pages/friend/gvideocallrev?id=" + e.id + "&mid=" + e.mid + "&uid=" + e
							.user.id + "&uname=" + e.user.name
					})
				}
			})

			sub("socket_gaudiocallinv", (e) => {
				const status = _this.$store.state.common.status
				console.log("statusstatus", status)
				if (status != "videocall") {
					_this.playSoundl()
					uni.navigateTo({
						url: "/pages/friend/gaudiocallrev?id=" + e.id + "&mid=" + e.mid + "&uid=" + e
							.user.id + "&uname=" + e.user.name
					})
				}
			})


			sub("socket_touser_videocall", (e) => {
				const status = _this.$store.state.common.status
				console.log("statusstatus", status)
				if (status != "videocall") {
					_this.playSoundl()
					uni.navigateTo({
						url: "/pages/friend/videocallrev?id=" + e.user.id + '&mid=' + e.mid
					})
				} else {
					_this.$socket.send({
						type: 'touser_videocallbusy_' + e.mid,
						data: {
							to: {
								id: e.user.id
							},
							mid: e.mid
						}
					})
					_this.$u.put(`chat/chat/changemessageinfo/${e.mid}`, {
						status: '忙线取消'
					})

				}

			})

			sub("socket_touser_audiocall", (e) => {
				const status = _this.$store.state.common.status
				console.log("statusstatus", status)
				if (status != "videocall") {
					_this.playSoundl()
					uni.navigateTo({
						url: "/pages/friend/audiocallrev?id=" + e.user.id + '&mid=' + e.mid
					})
				} else {
					_this.$socket.send({
						type: 'touser_audiocallbusy_' + e.mid,
						data: {
							to: {
								id: e.user.id
							},
							mid: e.mid
						}
					})
					_this.$u.put(`chat/chat/changemessageinfo/${e.mid}`, {
						status: '忙线取消'
					})

				}

			})

			sub("socket_kickoff", () => {
				auth.removeAuthorization()
				uni.showToast({
					title: "异地登录",
					icon: "none"
				})
				pub("logout", {})
				uni.redirectTo({
					url: "/pages/login/login"
				})
			})
			
			sub("logout", () => {
				uni.redirectTo({
					url:"/pages/login/login"
				})
			})

			sub("stopmusic", () => {
				_this.stopmusic()
			})

		},
		onShow: function() {
			console.log('App Show')
			showfront = true
			// #ifdef APP
			try{
				innerAudioContextdf.stop()
			}catch(e){
				//TODO handle the exception
			}
			// #endif
		},
		onHide: function() {
			console.log('App Hide')
			showfront = false
			// #ifdef APP
			this.playSounddf()
			// #endif
		},
		globalData: {

		},
		methods: {
			stopmusic: function() {
				try {
					const ret = commonData.getAudioCtx()
					ret.stop()
				} catch (e) {
					//TODO handle the exception
				}
			},
			playSounddf: function() {
				let url = '/static/music/df.m4a';
				innerAudioContextdf.autoplay = true;
				innerAudioContextdf.loop = true;
				innerAudioContextdf.src = url;
				innerAudioContextdf.onPlay(() => {
					console.log('开始播放DF');
				});
				innerAudioContextdf.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
			},
			playSoundl: function() {
				// #ifdef APP
				if(!showfront){
					return
				}
				// #endif
				const setting = this.$store.state.user.user
				if (!setting.msgmusic) {
					return
				}
				let url = '/static/music/' + 'vdcall' + '.mp3';
				const innerAudioContext = uni.createInnerAudioContext();
				commonData.setAudioCtx(innerAudioContext)
				innerAudioContext.autoplay = true;
				innerAudioContext.src = url;
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
			},
			playSound: function() {
				// #ifdef APP
				if(!showfront){
					return
				}
				// #endif
				const setting = this.$store.state.user.user
				if (!setting.msgmusic) {
					return
				}
				const soundMap = {
					qk: "轻快",
					lx: "流行",
					qr: "轻柔",
					dc: "短促",
				}
				let url = '/static/music/' + 'qk' + '.mp3';
				for (let k in soundMap) {
					if (soundMap[k] == setting.msgmusic) {
						url = '/static/music/' + k + '.mp3'
						break
					}
				}
				console.log(url)
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = url;
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res);
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	/*每个页面公共css */

	page {
		background: linear-gradient(to bottom, #e8eaf0, #eff0fb);
	}

	.indexcontent {
		width: 100%;
		display: flex;
		/* #ifdef APP */
		height: calc(100% - 60rpx - 46px);
		/* #endif */
		/* #ifdef H5 */
		height: calc(100% - 100rpx);
		/* #endif */
		background: linear-gradient(to bottom, #fff, #eff0fb);
		border-start-start-radius: 32rpx;
		border-start-end-radius: 32rpx;
		flex-shrink: 0;
		flex-direction: column;
		overflow: auto;
		padding: 20rpx;
	}

	.bgyxtag {
		padding: 5rpx;
		display: flex;
		align-items: center;
		justify-content: center;

	}
</style>